package com.aop;

import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;

/**
 * @author: cxt
 * @time: 2022/11/16
 */
@Component
public class MyTest {

    @RepeatableTest("测试的")
    @RepeatableTest("hello")
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public void test() throws NoSuchMethodException {
        // 获取重复注解中内容
        RepeatableTest[] tests = this.getClass().getMethod("test").getAnnotationsByType(RepeatableTest.class);
        System.out.println(Arrays.toString(tests));

    }

    public static void main(String[] args) throws NoSuchMethodException, IOException, ClassNotFoundException {
        /*Method[] methods = Test.class.getMethods();
        for (Method method : methods) {
            if ("test".equals(method.getName())) {
                Annotation[] annotations = method.getDeclaredAnnotations();
                System.out.println(Arrays.toString(annotations));
            }
        }

        // 获取注解中参数方式二
        Test test = new Test("namme");
        test.test();*/

        /* 测试jdk动态代理
        // 被代理的类
        Represented represented = new Represented();
        // 创建中间商类
        Middleman<Represented> representedMiddleman = new Middleman<Represented>(represented);
        // 创建代理对象
        Person person = (Person) Proxy.newProxyInstance(Person.class.getClassLoader(), new Class<?>[]{Person.class}, representedMiddleman);
        person.shopping();
        */

        /*
        // 序列化对象到磁盘文件
        Middleman<String> middleman = new Middleman<>("这是测试序列化");
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream out = null;
        try {
            fileOutputStream = new FileOutputStream("D://middleman.txt");
            out = new ObjectOutputStream(fileOutputStream);
            // 将对象写入文件
            out.writeObject(middleman);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
                fileOutputStream.close();
            }
        }
        */

        /*
        // 反序列化
        FileInputStream inputStream = new FileInputStream("D://middleman.txt");
        ObjectInputStream stream = new ObjectInputStream(inputStream);
        Middleman<String> middleman = (Middleman<String>) stream.readObject();
        System.out.println(middleman);
        */

    }

    @Test
    public void test3() {
        LinkedList<String> list = new LinkedList<>();
        list.add("测试的");
        int i = RandomUtils.nextInt(1, 15);
        System.out.println("随机数是：" + i);
        for (int j = 0; j <= i; j++) {
            list.add("das" + j);
        }

        // 如果对象不为null，会进入ifPresent中去，这里可以调用本类中方法，直接把存在的对象传过去
        Optional.of(list).ifPresent(System.out::println);

        // 如果对象为null时候该做啥时候使用orElse或者orElseget方法,可以传入当前类中的其他方法的调用
        Optional.of(list).orElse(new LinkedList<>()).add("这里是新建的");

        // 还可以使用orElseget,和上面的区别就是有无返回值
        Optional.of(list).orElseGet(LinkedList::new).add("这里是新建的");
    }

    @Test
    public void test4() {
        // 反射创建对象
        try {
            // 通过对象名来创建
            TestImpl test = TestImpl.class.newInstance();

            TestImpl test1 = new TestImpl("测试的对象");
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("D://Test"));
            outputStream.writeObject(test1);
            outputStream.close();

        } catch (InstantiationException | IllegalAccessException | IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void tes() {
        int recursion = recursion(0, 1, 0, 6);
        System.out.println(recursion);
    }

    /**
     * 输出波非那切数列
     *
     * @param one            前一项
     * @param two            后一项
     * @param frequency      当前递归次数
     * @param totalFrequency 总递归次数
     * @return 从0开始第totalFrequency项的值
     */
    public int recursion(int one, int two, int frequency, int totalFrequency) {
        int i = one + two;
        if (frequency >= totalFrequency) {
            return one;
        } else {
            i = recursion(two, i, (frequency + 1), totalFrequency);
        }
        return i;
    }

    public void te(ArrayList<String> te) {
        te.add("aaaa");
    }

    @Test
    public void tests() {


    }

}
